package sdl2_impl

import (
	"github.com/gonutz/go-sdl2/sdl"
	"github.com/gonutz/go-sdl2/sdl_mixer"
	"unsafe"
)

type Speakers struct {
	chunk  *mix.Chunk
	window *sdl.Window
}

func NewSpeakers(w *sdl.Window) *Speakers {
	return &Speakers{window: w}
}

func (s *Speakers) Init() {
	mix.OpenAudio(44100, mix.DEFAULT_FORMAT, 1, 512)
	wavLoader := sdl.RWFromMem(unsafe.Pointer(&SineWav[0]), len(SineWav))
	s.chunk = mix.LoadWAV_RW(wavLoader, 0)
	s.chunk.PlayChannel(0, -1)
	s.BeQuiet()
}

func (s *Speakers) Release() {
	s.chunk.Free()
	mix.CloseAudio()
}

func (s *Speakers) Beep() {
	mix.SetVolume(0, mix.MAX_VOLUME)
	s.window.SetTitle("BEEEEEEEEEEEEEEEEEEEEEEEEP")
}

func (s *Speakers) BeQuiet() {
	mix.SetVolume(0, 0)
	s.window.SetTitle("shhh")
}
